package cn.yhjz.generator.util;

import cn.yhjz.generator.entity.SysGenModel;
import cn.yhjz.generator.entity.SysGenModelProp;
import cn.yhjz.generator.entity.SysGenModelRender;
import cn.yhjz.generator.entity.SysGenTemplate;
import cn.yhjz.generator.service.ISysGenModelPropService;
import cn.hutool.core.util.StrUtil;
import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class TemplateUtil {
    @Autowired
    private ISysGenModelPropService propService;

    /**
     * 装配模型和模板，返回结果字符串
     * @param sysGenModel
     * @param sysGenTemplate
     * @throws IOException
     * @throws TemplateException
     */
    public String assemble(SysGenModel sysGenModel, SysGenTemplate sysGenTemplate) throws IOException, TemplateException {
        return this.assembleTemplate(sysGenModel, sysGenTemplate.getTemplate());
    }

    /**
     * 渲染模板字符串
     * @param sysGenModel
     * @param templateContent
     * @return
     */
    public String assembleTemplate(SysGenModel sysGenModel,String templateContent) throws IOException, TemplateException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
        SysGenModelRender sysGenModelRender = new SysGenModelRender();
        BeanUtils.copyProperties(sysGenModel, sysGenModelRender);
        sysGenModelRender.getObjectName();
        SysGenModelProp sysGenModelProp = new SysGenModelProp();
        sysGenModelProp.setModelCode(sysGenModel.getModelCode());
        List<SysGenModelProp> modelPropList = propService.select(sysGenModelProp);
        StringBuffer sb = new StringBuffer();
        String keyColumn = "id";
        String keyProp = "id";
        for (SysGenModelProp prop : modelPropList) {
            sb.append(prop.getColumnName() + ",");
            if ("1".equals(prop.getIsPk())) {
                keyColumn = prop.getJavaField();
                keyProp = prop.getColumnName();
            }
        }
        String columnJoins = sb.toString();
        if (StrUtil.isNotEmpty(columnJoins)) {
            columnJoins = columnJoins.substring(0, columnJoins.length() - 1);
        }
        //按道理讲，下面的Configuration对象应该是全局的
        // 第一步：创建一个Configuration对象，直接new一个对象。构造方法的参数就是freemarker对应的版本号。
        Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
        configuration.setDefaultEncoding("utf-8");
        //设置从String加载模板
        StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();
        //给模板一个名字
        stringTemplateLoader.putTemplate("myTemplate", templateContent);
        configuration.setTemplateLoader(stringTemplateLoader);
        configuration.clearTemplateCache();
        Template template = configuration.getTemplate("myTemplate", "utf-8");
        StringWriter sw = new StringWriter();
        Map<String, Object> renderParam = new HashMap<>();
        renderParam.put("model", sysGenModelRender);
        renderParam.put("columnJoins", columnJoins);
        renderParam.put("keyColumn", keyColumn);
        renderParam.put("keyProp", keyProp);
        renderParam.put("props", modelPropList);
        renderParam.put("now", simpleDateFormat.format(new Date()));
        template.process(renderParam, sw);
        return sw.toString();
    }
}
